<?php
namespace Store\Model;

use Think\Model;

class CartModel extends Model
{
    protected $cart = 0;
    protected $m_cart_list = array();

    function _initialize()
    {
        $this->cart = json_decode(cookie('cart'), true);
        if (!is_array($this->cart)) {
            $this->cart = array(
                'cart_tag' => 1,
                'items' => array()
            );
        }
    }

    function getList($map = '', $num = 10, $order = 'cTime desc')
    {

    }

    function getLimit($map = '', $num = 10, $order = 'cTime desc')
    {
//        $items = $this->cart['items'];
//        dump($items);exit;
        $items = D('store_order_car')->where(array('uid' => is_login()))->select();
        foreach ($items as $key => $v) {
            $items[$key]['good'] = D('Info')->getById($v['goods_id']);
        }
        return $items;
    }

    /**
     *
     * 添加一个物品
     * @param unknown_type $item
     * good_id
     * count
     * cTime
     */
    function addItem($item)
    {

        if ($item['count'] <= 0) {
            return false;
        }

        $added = false;

        foreach ($this->cart['items'] as $key => $v) {
            if ($v['good_id'] == $item['good_id']) {
                $this->cart['items'][$key]['count'] += $item['count'];
                $added = true;
            }
        }
        $old = D('store_order_car')->where(array('uid' => is_login(), 'goods_id' => $item['good_id']))->find();
//                dump( $v['good_id']);exit;
        if ($old) {
            $res=  D('store_order_car')->where(array('uid' => is_login(), 'goods_id' => $item['good_id']))->save(array('create_time' => time(), 'count' => $old['count'] + $item['count']));
        } else {
            $res=  D('store_order_car')->add(array('uid' => is_login(), 'goods_id' => $item['good_id'], 'create_time' => time(), 'count' => $item['count']));
        }
        if ($added == false) {
            $this->cart['items'][] = $item;
        }
        $this->_save();
        return $res;
    }

    function clear()
    {
        $this->cart = null;
        $this->_save();
    }

    function removeItem($good_id)
    {

        foreach ($this->cart['items'] as $key => $v) {
            if ($v['good_id'] == $good_id) {
                D('store_order_car')->where(array('uid' => is_login(), 'goods_id' => $v['good_id']))->delete();
                unset($this->cart['items'][$key]);
            }
        }
        $this->_save();
    }
    function clearCar()
    {
        D('store_order_car')->where(array('uid' => is_login()))->delete();
    }
    function setItemCount($good_id, $count)
    {
        foreach ($this->cart['items'] as $key => $v) {
            if ($v['good_id'] == $good_id) {
                $this->cart['items'][$key]['count'] = $count;
                D('store_order_car')->where(array('uid' => is_login(), 'good_id' => $v['good_id']))->setField('count', $count);

            }
        }
        $this->_save();
    }

    function _save()
    {
        $json_cart = json_encode($this->cart);
        cookie('cart', $json_cart, 3600 * 24 * 30);
    }

    function getById($id)
    {
    }

}